package com.traffic.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.traffic.constant.Status;
import com.traffic.entity.Param;
import com.traffic.entity.QueryResult;
import com.traffic.entity.Score;
import com.traffic.entity.UserInfo;
import com.traffic.model.ApiResponse;
import com.traffic.model.ScoreResult;
import com.traffic.service.ParamService;
import com.traffic.service.ScoreService;
import com.traffic.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(tags = "后台分数查看相关接口")
@RestController
@RequestMapping(value = "/score")
@Slf4j
public class ScoreController {

    @Autowired
    private ScoreService scoreService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private ParamService paramService;

    @ApiOperation(value = "【分数】", notes = "用户id查分")
    @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "userId", value = "用户id", required = true)
    @GetMapping("/userscore/{userId}")
    public ApiResponse UserScore(@PathVariable Long userId) {

        QueryWrapper<Score> wrapper = new QueryWrapper<>();
        List<Score> userscores= scoreService.list(wrapper.eq("user_id",userId));

        return ApiResponse.ofStatus(Status.OK,userscores);
    }

    @ApiOperation(value = "【分数】", notes = "全部分数表")
   // @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "userId", value = "用户id", required = true)
    @GetMapping("/all/{page}")
    public ApiResponse AllScore(@PathVariable Integer page) {

        QueryWrapper<Score> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("createtime");

        Integer count = scoreService.count();
        IPage<Score> scoreIPage = new Page<Score>(page, 10,count);
        IPage<Score> score = scoreService.page(scoreIPage,wrapper);
        List<Score> scores= score.getRecords();
        List<ScoreResult> scoreResults = new ArrayList<>();
        List<Param> params = paramService.queryTargetParam("B");
        for (Score s:scores) {
            ScoreResult temp = new ScoreResult();
            temp.setScore(s);
            UserInfo userInfo = userInfoService.getUserInfoByUserId(s.getUserId());
            temp.setUsername(userInfo.getPhone());
            temp.setTotal(score.getTotal());


            temp.setPx1(s.getX1()*params.get(0).getValue());
            temp.setPx2(s.getX2()*params.get(1).getValue());
            temp.setPx3(s.getX3()*params.get(2).getValue());
            temp.setPx4(s.getX4()*params.get(3).getValue());
            temp.setPx5(s.getX5()*params.get(4).getValue());
            temp.setPx6(s.getX6()*params.get(5).getValue());
            temp.setPx7(s.getX7()*params.get(6).getValue());
            temp.setPx8(s.getX8()*params.get(7).getValue());
            temp.setPx9(s.getX9()*params.get(8).getValue());
            temp.setPx10(s.getX10()*params.get(9).getValue());
            temp.setPx11(s.getX11()*params.get(10).getValue());
            temp.setPx12(s.getX12()*params.get(11).getValue());
            temp.setPx13(s.getX13()*params.get(12).getValue());
            temp.setPx14(s.getX14()*params.get(13).getValue());
            temp.setPx15(s.getX15()*params.get(14).getValue());
            temp.setPx16(s.getX16()*params.get(15).getValue());

            scoreResults.add(temp);

        }

        return ApiResponse.ofStatus(Status.OK,scoreResults);
    }

    @ApiOperation(value = "【分数】", notes = "用户id查分")
    @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "userId", value = "用户id", required = true)
    @PostMapping("/user")
    public ApiResponse PhoneUserScore(@RequestBody Map<String,String> param) throws ParseException {

        UserInfo userInfo = userInfoService.queryInfo(param.get("phone"));
        String startdate = param.get("starttime");
        String enddate = param.get("endtime");

        List<Score> userscores= scoreService.queryByPhoneAndTime(userInfo.getUserId().toString(),startdate,enddate);

        List<ScoreResult> scoreResults = new ArrayList<>();
        for (Score s:userscores) {
            ScoreResult temp = new ScoreResult();
            temp.setScore(s);
            temp.setUsername(userInfo.getPhone());

            scoreResults.add(temp);

        }

        return ApiResponse.ofStatus(Status.OK,scoreResults);
    }


}
